package 递归;
/*
* 迷宫问题
* */
public class MiGong {
    static int[][] arr = new int[8][7];
    public static void main(String[] args) {
        InitMiGong();
        System.out.println(setWay2(arr,1,1));
        display(arr);
    }

    /*
    * 策略: 下->右->上->左
    * 0代表没走过 1代表墙 2代表通路 3代表死路
    * */
    /**
     * @param map 地图
     * @param i   起始地下标
     * @param j   起始地下标
     * @return 如果找到路返回 true 反之返回 false
     */
    public static boolean setWay(int[][] map,int i,int j){
        if(((i == 6&& j == 4) || (i == 5&& j == 5)) && map[i][j] != 0){
            map[i][j] = 3;
        }else {
            map[i][j] = 2;
        }
        if(map[6][4] == 3 && map[5][5] == 3){
            return false;
        }

        if(map[i+1][j] == 0 || map[i+1][j] == 2){
            i = i+1;
        }else if(map[i][j+1] == 0 || map[i][j+1] == 2){
            j = j+1;
        }else if(map[i-1][j] == 0 || map[i-1][j] == 2){
            i = i-1;
        }else if(map[i][j-1] == 0 || map[i][j-1] == 2){
            j = j-1;
        }else{
            return false;
        }

        if(i == 6&& j == 5){
            map[i][j] = 2;
            return true;
        }else{
            return setWay(map,i,j);
        }
    }

    public static boolean setWay2(int[][] map,int i,int j){
        if(map[6][5] == 2){
            return true;
        }else{
            if(map[i][j] == 0) {// 当前这个点还没有走过
                map[i][j] = 2;
                if (setWay2(map, i + 1, j)) {
                    return true;
                } else if (setWay2(map, i, j + 1)) {
                    return true;
                } else if (setWay2(map, i - 1, j)) {
                    return true;
                } else if (setWay2(map, i, j - 1)) {
                    return true;
                } else {
                    map[i][j] = 3;
                    return false;
                }
            }else{
                return false;
            }
        }
    }

    public static void InitMiGong(){
        int[][] newArr = new int[8][7];
        // 上下墙
        for (int i = 0; i < 7; i++) {
            newArr[0][i] = 1;
            newArr[7][i] = 1;
        }

        // 左右墙
        for (int i = 0; i < 8; i++) {
            newArr[i][0] = 1;
            newArr[i][6] = 1;
        }

        newArr[3][1] = 1;
        newArr[3][2] = 1;
        arr = newArr;
    }

    public static void display(int[][] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
}


